﻿[<AutoOpen>]
module SiteCommon
open IntelliFactory.WebSharper
open IntelliFactory.WebSharper.Html
open IntelliFactory.WebSharper.JQuery
open IntelliFactory.WebSharper.JQuery.Mobile

type MainPages = Policies | Claims | Billing | ContactUs

[<JavaScript>]
module ConfirmationState =
    let g_yes = ref ""
    let g_no = ref ""
    let g_msg = ref ""
    let g_next = ref ""

[<JavaScript>]
module Nav =
    //std page transitions
    let SlideRight = Some(ChangePageConfig(Transition="slide"))
    let SlideLeft  = Some(ChangePageConfig(Transition="slide", Reverse=true))
    let Pop        = Some(ChangePageConfig(Transition="pop"))

    type Direction = Forward | Back

    let private  n_prev = ref ""
    let private n_dir = ref Forward

    let direction() = !n_dir

    let transitionWith (txnOpt:ChangePageConfig option) (p:JQuery.JQuery) =
        let toPageId = p.Attr("id")
        let fromPageId = JQuery.Mobile.Mobile.Instance.ActivePage.Attr("id")
        if toPageId <> fromPageId then
            n_prev := fromPageId
            n_dir := Forward
        match txnOpt with
        | Some txn -> JQuery.Mobile.Mobile.Instance.ChangePage(p,txn)
        | None     -> JQuery.Mobile.Mobile.Instance.ChangePage p

    let transition (p:JQuery.JQuery) = transitionWith None p

    let private back() =
        if !n_prev <> "" then
            n_dir := Back
            JQuery.Of("#" + !n_prev) |> JQuery.Mobile.Mobile.Instance.ChangePage
        else
            ()

    let showDialog(msg) = 
        ConfirmationState.g_msg := msg
        JQuery.Of("#messageDialog") |> transitionWith Pop

    let dialogClose() = back()

    let showNotification(msg,nextPage) = 
        ConfirmationState.g_msg := msg
        ConfirmationState.g_next := nextPage
        let cp = ChangePageConfig()
        cp.Transition <- "pop"
        JQuery.Mobile.Mobile.Instance.ChangePage(JQuery.Of("#notificationDialog"), cp)

    let notificationClose() =
            n_dir := Forward
            JQuery.Of(!ConfirmationState.g_next) |> JQuery.Mobile.Mobile.Instance.ChangePage


[<JavaScript>] 
let OnShow f (e:#IPagelet) = JQuery.Mobile.Page.Show.On(JQuery.Of(e.Body),f)

[<JavaScript>] 
let OnBeforeShow f (e:#IPagelet) = JQuery.Mobile.Page.BeforeShow.On(JQuery.Of(e.Body),f)

[<JavaScript>] 
let OnCreate f (e:#IPagelet) = JQuery.Mobile.Page.Create.On(JQuery.Of(e.Body),f)

[<JavaScript>]
let rec footer page =
    Div [
        Id ("footer"+page.ToString()) //footers for each page seem to require unique ids to work properly
        HTML5.Attr.Data "role" "footer"
        HTML5.Attr.Data "position" "fixed"
        Attr.Class "ui-bar"
//        HTML5.Attr.Data "id" "foot"
//        HTML5.Attr.Data "theme" "b"
        ] -< [
            A ([HRef "#"; HTML5.Attr.Data "role" "button"; HTML5.Attr.Data "icon" "home"; HTML5.Attr.Data "iconpos" "top"]
                @
                [if page = MainPages.Policies then yield Attr.Class "ui-btn-active"])
                -< [Text "Policies"]
            |>! OnClick (fun _ _ -> 
                JQuery.Of("#policies") 
                |> Nav.transition)

            A ([HRef "#"; HTML5.Attr.Data "role" "button"; HTML5.Attr.Data "icon" "star"; HTML5.Attr.Data "iconpos" "top"]
                @
                [if page = MainPages.Claims then yield Attr.Class "ui-btn-active"])
                -< [Text "Claims"]
            |>! OnClick (fun _ _ -> 
                JQuery.Of("#claims") 
                |> Nav.transition)

            A ([HRef "#"; HTML5.Attr.Data "role" "button"; HTML5.Attr.Data "icon" "grid"; HTML5.Attr.Data "iconpos" "top"]
                @
                [if page = MainPages.Billing then yield Attr.Class "ui-btn-active"])
                -< [Text "Billing"]
            |>! OnClick (fun _ _ -> 
                JQuery.Of("#billing") 
                |> Nav.transition)

            A ([HRef "#"; HTML5.Attr.Data "role" "button"; HTML5.Attr.Data "icon" "info"; HTML5.Attr.Data "iconpos" "top"] 
                @
                [if page = MainPages.ContactUs then yield Attr.Class "ui-btn-active"])
            -< [Text "Contact Us"]
            |>! OnClick (fun _ _ -> 
                JQuery.Of("#contactus") 
                |> Nav.transition)
        ]
        |>! OnAfterRender (fun d -> FixedToolbar.Init(JQuery.Of(d.Dom), FixedToolbarConfig(TapToggle=false)))
(*
[<JavaScript>]
let rec footer page =
    Div [
        Id ("footer"+page.ToString()) //footers for each page seem to require unique ids to work properly
        HTML5.Attr.Data "role" "footer"
        HTML5.Attr.Data "position" "fixed"
//        HTML5.Attr.Data "id" "foot"
//        HTML5.Attr.Data "theme" "b"
        ] -< [
        Div [HTML5.Attr.Data "role" "navbar"] -< [
            UL[
                LI [] -<  [
                    A ([HRef "#"; HTML5.Attr.Data "role" "button"; HTML5.Attr.Data "icon" "home"]
                        @
                        [if page = MainPages.Policies then yield Attr.Class "ui-btn-active ui-state-persist"])
                        -< [Text "Policies"]

                    |>! OnClick (fun _ _ -> 
                        JQuery.Of("#policies") 
                        |> Nav.forward)
                ]
                LI [] -<  [
                    A ([HRef "#"; HTML5.Attr.Data "role" "button"]
                        @
                        [if page = MainPages.Claims then yield Attr.Class "ui-btn-active ui-state-persist"])
                        -< [Text "Claims"]

                    |>! OnClick (fun _ _ -> 
                        JQuery.Of("#claims") 
                        |> Nav.forward)
                ]
                LI [] -<  [
                   A ([HRef "#"; HTML5.Attr.Data "role" "button"]
                        @
                        [if page = MainPages.Billing then yield Attr.Class "ui-btn-active ui-state-persist"])
                        -< [Text "Billing"]

                    |>! OnClick (fun _ _ -> 
                        JQuery.Of("#billing") 
                        |> Nav.forward)
                ]
                LI [] -<  [
                    A ([HRef "#"; HTML5.Attr.Data "role" "button"] 
                        @
                        [if page = MainPages.ContactUs then yield Attr.Class "ui-btn-active ui-state-persist"])
                   -< [Text "Contact Us"]
                    |>! OnClick (fun _ _ -> 
                        JQuery.Of("#contactus") 
                        |> Nav.forward)
                ]
            ]
        ]
    ]
*)

[<JavaScript>]
let homeButton() =
    A [
        HTML5.Attr.Data "role" "button"
        HTML5.Attr.Data "icon" "home"
        HTML5.Attr.Data "mini" "true"
        HTML5.Attr.Data "iconpos" "notext"]
    |>! OnClick (fun _ _ -> JQuery.Of("#policies") |> Nav.transitionWith Nav.Pop)


[<JavaScript>]
let dialogPage() =
        Div [HTML5.Attr.Data "role" "page"; Id "messageDialog"; HTML5.Attr.Data "theme" "e"] -< [
                
                H1 [Id "messageText"] -< [Text "..."]

                A [ HTML5.Attr.Data "role" "button"; 
                    HTML5.Attr.Data "icon" "back"
                ] -< [Text "Close"]
                |>! OnClick (fun _ _ -> Nav.dialogClose())

            ]
        |>! OnBeforeShow (fun p ->
            JQuery.Of("#messageText").Text(!ConfirmationState.g_msg) |> ignore)

[<JavaScript>]
let notificationPage() =
        Div [HTML5.Attr.Data "role" "page"; Id "notificationDialog"; HTML5.Attr.Data "theme" "e"] -< [
                
                H1 [Id "notificationMessage"] -< [Text "..."]

                A [ HTML5.Attr.Data "role" "button"; 
                    HTML5.Attr.Data "icon" "check"
                ] -< [Text "Ok"]
                |>! OnClick (fun _ _ -> Nav.notificationClose())

            ]
        |>! OnBeforeShow (fun p ->
            JQuery.Of("#notificationMessage").Text(!ConfirmationState.g_msg) |> ignore)


[<JavaScript>]
let ShowBusy (jq:IntelliFactory.WebSharper.JQuery.JQuery) =
    let dom = Div[ Img[Src "Images/ajax-loader.gif"]]
    jq.Html(dom.Html)
    |> ignore
    